//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Floor.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Instruments/CapFloor.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CFloor::CFloor (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::Core::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer) : CCapFloor(CFloor::typeid)
{
    try
    {
#ifdef HANDLE
        _phFloor = NULL;
#endif
        floatingLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIfloatingLeg = floatingLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCfloatingLeg = safe_cast<CCashFlowVector^>(_NCIfloatingLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _floatingLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCfloatingLeg->GetShared ());
        exerciseRates->Lock();
        INativeVector<Double>^ _NCIexerciseRates = exerciseRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseRates = safe_cast<CDoubleVector^>(_NCIexerciseRates);
        std::vector<QuantLib::Rate>& _exerciseRates = static_cast<std::vector<QuantLib::Rate>&> (_NCexerciseRates->GetReference ());
        _ppFloor = new boost::shared_ptr<QuantLib::Floor> (new QuantLib::Floor ( _floatingLeg,  _exerciseRates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppFloor)->setPricingEngine (_QL_Pricer);
        SetCapFloor (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (floatingLeg != nullptr) floatingLeg->Unlock();    //not optional
        if (exerciseRates != nullptr) exerciseRates->Unlock();    //not optional
    }
}
Cephei::QL::Instruments::CFloor::CFloor (boost::shared_ptr<QuantLib::Floor>& childNative, Object^ owner) : CCapFloor(CFloor::typeid)
{
#ifdef HANDLE
	_phFloor = NULL;
#endif
	_ppFloor = &childNative;
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
}
Cephei::QL::Instruments::CFloor::CFloor (QuantLib::Floor& childNative, Object^ owner) : CCapFloor(CFloor::typeid)
{
#ifdef HANDLE
	_phFloor = NULL;
#endif
	_ppFloor = new boost::shared_ptr<QuantLib::Floor> (&childNative);
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
    _FloorOwner = owner;
    _CapFloorOwner = owner;
}

Cephei::QL::Instruments::CFloor::CFloor (CFloor^ copy) : CCapFloor(CFloor::typeid)
{
#ifdef HANDLE
	_phFloor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFloor = new boost::shared_ptr<QuantLib::Floor> (copy->GetShared());
        _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
    }
}
Cephei::QL::Instruments::CFloor::CFloor (PLATFORM::Type^ t) : CCapFloor(CFloor::typeid)
{
#ifdef HANDLE
	_phFloor = NULL;
#endif
	if (!t->IsSubclassOf(CFloor::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CFloor::CFloor (QuantLib::Handle<QuantLib::Floor>& childNative, Object^ owner)  : CCapFloor(CFloor::typeid)
{
	_phFloor = &childNative;
	_ppFloor = &static_cast<boost::shared_ptr<QuantLib::Floor>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
    _FloorOwner = owner;
}
Cephei::QL::Instruments::CFloor::CFloor (QuantLib::Handle<QuantLib::Floor> childNative)  : CCapFloor(CFloor::typeid)
{
	_phFloor = &childNative;
	_ppFloor = &static_cast<boost::shared_ptr<QuantLib::Floor>>(childNative.currentLink());
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CFloor::CFloor (QuantLib::Floor childNative)  : CCapFloor(CFloor::typeid)
{
#ifdef HANDLE
	_phFloor = NULL;
#endif
	_ppFloor = new boost::shared_ptr<QuantLib::Floor> (new QuantLib::Floor (childNative));
    _ppCapFloor = new boost::shared_ptr<QuantLib::CapFloor> (boost::dynamic_pointer_cast<QuantLib::CapFloor> (*_ppFloor));
}
#endif

Cephei::QL::Instruments::CFloor::~CFloor ()
{
    if (_ppFloor != NULL)
    {
	    delete _ppFloor;
        _ppFloor = NULL;
    }
}
Cephei::QL::Instruments::CFloor::!CFloor ()
{
    if (_ppFloor != NULL)
    {
	    delete _ppFloor;
    }
}
QuantLib::Floor& Cephei::QL::Instruments::CFloor::GetReference ()
{
    if (_ppFloor == NULL) throw REFNEW NativeNullException ();
	return **_ppFloor;
}
boost::shared_ptr<QuantLib::Floor>& Cephei::QL::Instruments::CFloor::GetShared ()
{
    if (_ppFloor == NULL) throw REFNEW NativeNullException ();
	return *_ppFloor;
}
QuantLib::Floor* Cephei::QL::Instruments::CFloor::GetPointer ()
{
    if (_ppFloor == NULL) throw REFNEW NativeNullException ();
	return &**_ppFloor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Floor>& Cephei::QL::Instruments::CFloor::GetHandle ()
{
	if (_phFloor == NULL)
	{
		_phFloor = new Handle<QuantLib::Floor> (*_ppFloor);
	}
	return *_phFloor;
}
#endif
bool Cephei::QL::Instruments::CFloor::HasNative () 
{
	return (_ppFloor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IFloor^ Cephei::QL::Instruments::CFloor_Factory::Create (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ floatingLeg, Cephei::Core::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CFloor ( floatingLeg,  exerciseRates,  QL_Pricer);
}
